<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/xhtml;charset=UTF-8"/>
<meta http-equiv="X-UA-Compatible" content="IE=9"/>
<meta name="generator" content="Doxygen 1.8.13"/>
<meta name="viewport" content="width=device-width, initial-scale=1"/>
<title>C# CanlibTutorial, VS2017</title>
<link href="tabs.css" rel="stylesheet" type="text/css"/>
<script type="text/javascript" src="jquery.js"></script>
<script type="text/javascript" src="dynsections.js"></script>
<link href="navtree.css" rel="stylesheet" type="text/css"/>
<script type="text/javascript" src="resize.js"></script>
<script type="text/javascript" src="navtreedata.js"></script>
<script type="text/javascript" src="navtree.js"></script>
<script type="text/javascript">
  $(document).ready(initResizable);
</script>
<link href="search/search.css" rel="stylesheet" type="text/css"/>
<script type="text/javascript" src="search/searchdata.js"></script>
<script type="text/javascript" src="search/search.js"></script>
<link href="doxygen.css" rel="stylesheet" type="text/css" />
</head>
<body>
<div id="top"><!-- do not remove this div, it is closed by doxygen! -->
<div id="titlearea">
<table cellspacing="0" cellpadding="0">
 <tbody>
 <tr style="height: 56px;">
  <td id="projectlogo"><img alt="Logo" src="kvaser.gif"/></td>
 </tr>
 </tbody>
</table>
</div>
<!-- end header part -->
<!-- Generated by Doxygen 1.8.13 -->
<script type="text/javascript">
var searchBox = new SearchBox("searchBox", "search",false,'Search');
</script>
<script type="text/javascript" src="menudata.js"></script>
<script type="text/javascript" src="menu.js"></script>
<script type="text/javascript">
$(function() {
  initMenu('',true,false,'search.php','Search');
  $(document).ready(function() { init_search(); });
});
</script>
<div id="main-nav"></div>
</div><!-- top -->
<div id="side-nav" class="ui-resizable side-nav-resizable">
  <div id="nav-tree">
    <div id="nav-tree-contents">
      <div id="nav-sync" class="sync"></div>
    </div>
  </div>
  <div id="splitbar" style="-moz-user-select:none;" 
       class="ui-resizable-handle">
  </div>
</div>
<script type="text/javascript">
$(document).ready(function(){initNavTree('page_tutorial_csharp_canlib_vs2017.html','');});
</script>
<div id="doc-content">
<!-- window showing the filter options -->
<div id="MSearchSelectWindow"
     onmouseover="return searchBox.OnSearchSelectShow()"
     onmouseout="return searchBox.OnSearchSelectHide()"
     onkeydown="return searchBox.OnSearchSelectKey(event)">
</div>

<!-- iframe showing the search results (closed by default) -->
<div id="MSearchResultsWindow">
<iframe src="javascript:void(0)" frameborder="0" 
        name="MSearchResults" id="MSearchResults">
</iframe>
</div>

<div class="header">
  <div class="headertitle">
<div class="title">C# CanlibTutorial, VS2017 </div>  </div>
</div><!--header-->
<div class="contents">
<div class="toc"><h3>Table of Contents</h3>
<ul><li class="level1"><a href="#section_tutorial_csharp_canlib">Setting up a project to use CANlib</a><ul><li class="level2"><a href="#section_tutorial_csharp_canlib_project">Creating an empty project</a></li>
<li class="level2"><a href="#section_tutorial_csharp_add_canlib">Adding CANlib</a></li>
</ul>
</li>
<li class="level1"><a href="#section_tutorial_csharp_code">Adding the code</a></li>
<li class="level1"><a href="#section_tutorial_csharp_compiling64">Setting up 64 bit compiling</a></li>
</ul>
</div>
<div class="textblock"><p>This tutorial will take you through setting up a Visual Studio 2017 project and creating an application that will be able to list connected devices.</p>
<h1><a class="anchor" id="section_tutorial_csharp_canlib"></a>
Setting up a project to use CANlib</h1>
<p>In this section we will enable the usage of Kvaser CANlib in Visual Studio 2017 when creating a C# project which can be used to build both WIN32 and WIN64 applications. We will also handle the platform settings: x86 and x64.</p>
<p>First we must have the "Kvaser Drivers for Windows" and "Kvaser CANlib SDK" installed, see <a class="el" href="section_install_windows.html">Installing on Windows</a> for details on how to download and install these.</p>
<p>Now we are ready to start Visual Studio 2017 (C#) and create our project (I am using "Microsoft Visual Studio Community 2017").</p>
<h2><a class="anchor" id="section_tutorial_csharp_canlib_project"></a>
Creating an empty project</h2>
<p>After starting Visual Studio 2017 (C#), we create a new project by selecting "File–&gt;New-&gt;Project...". Choose to create a "Console App, Visual C#", name it "CanlibTutorial" (the Solution will now also be named "CanlibTutorial"), and press "OK".</p>
<div class="image">
<img src="cs_tutorial_1_create_project.png" alt="cs_tutorial_1_create_project.png" width="600px"/>
<div class="caption">
Creating the CanlibTutorial project</div></div>
<p>You will now be given an empty main function that does nothing.</p>
<div class="image">
<img src="cs_tutorial_2_empty_project.png" alt="cs_tutorial_2_empty_project.png" width="600px"/>
<div class="caption">
The CanlibTutorial project</div></div>
<h2><a class="anchor" id="section_tutorial_csharp_add_canlib"></a>
Adding CANlib</h2>
<p>Now we need to add CANlib to the project.</p>
<p>Open the reference manager (Project -&gt; Add Reference) and press browse to locate and add the 32 bit or 64 bit library to the reference manager using one of the paths below.</p>
<dl class="section note"><dt>Note</dt><dd>CANLib is by default installed to C:\Program Files (x86)\Kvaser</dd></dl>
<ul>
<li>Using 32 bit <br />
 Add path to LIB(x86) (..\Kvaser\Canlib\dotnet\win32\netstandard2.0\Kvaser.CanLib.dll)</li>
<li>Using 64 bit <br />
 Add path to LIB(x64) (..\Kvaser\Canlib\dotnet\x64\netstandard2.0\Kvaser.CanLib.dll)</li>
</ul>
<dl class="section note"><dt>Note</dt><dd>This applies to all libraries in the canlibSDK (<a class="el" href="page_canlib.html">CAN bus API (CANlib)</a>, <a class="el" href="page_linlib.html">LIN bus API (LINlib)</a>, <a class="el" href="page_kvadblib.html">Database API (kvaDbLib)</a>, <a class="el" href="page_kvmlib.html">Memorator API (kvmlib)</a>, <a class="el" href="page_kvamemolibxml.html">Memorator XML API (kvaMemoLibXML)</a>, <a class="el" href="page_kvlclib.html">Converter API (kvlclib)</a>, <a class="el" href="page_kvrlib.html">Remote Device API (kvrlib)</a>). Make sure not to mix 64 bit and 32 bit versions of the canlibSDK libraries.</dd></dl>
<div class="image">
<img src="cs_tutorial_3_references_browse.png" alt="cs_tutorial_3_references_browse.png"/>
<div class="caption">
Press "Browse" to locate and add references</div></div>
<p> After adding the reference to the reference manager, select the checkbox for the library you want to add to your project and press "OK". </p><dl class="section note"><dt>Note</dt><dd>Only one of the Kvaser.CanLib files can be added to a project at a time, if you wish to switch the current Kvaser.CanLib file you first need to delete it from the project and then add the replacement.</dd></dl>
<div class="image">
<img src="cs_tutorial_4_references_select.png" alt="cs_tutorial_4_references_select.png"/>
<div class="caption">
The Reference manager after adding the library file</div></div>
<p> Kvaser.CanLib should now be under References in the project.</p>
<div class="image">
<img src="cs_tutorial_5_references_added.png" alt="cs_tutorial_5_references_added.png"/>
<div class="caption">
Kvaser.CanLib has successfully been added to the project</div></div>
<h1><a class="anchor" id="section_tutorial_csharp_code"></a>
Adding the code</h1>
<p>The last step is to add <code>using Kvaser.CanLib;</code> to Program.cs. We do this as well as enter some more code...</p>
<div class="fragment"><div class="line"><span class="comment">//-----------------------------------------------------------</span></div><div class="line"><span class="comment">// This is a sample program for Visual Studio 2017 CANlib tutorial. </span></div><div class="line"><span class="comment">// It prints a list of connected CAN interfaces.</span></div><div class="line"><span class="comment">//</span></div><div class="line"><span class="comment">// For further information please refer to the tutorial section of the CANlib documentation.</span></div><div class="line"><span class="comment">//-----------------------------------------------------------</span></div><div class="line"></div><div class="line"><span class="keyword">using</span> System;</div><div class="line"><span class="keyword">using</span> System.Collections.Generic;</div><div class="line"><span class="keyword">using</span> System.Linq;</div><div class="line"><span class="keyword">using</span> System.Text;</div><div class="line"><span class="keyword">using</span> System.Threading.Tasks;</div><div class="line"></div><div class="line"><span class="keyword">using</span> Kvaser.CanLib;</div><div class="line"></div><div class="line"><span class="keyword">namespace </span>CanlibTutorial</div><div class="line">{</div><div class="line">    <span class="keyword">class </span>Program</div><div class="line">    {</div><div class="line">        <span class="comment">// When called CheckForError will check for and print any error.</span></div><div class="line">        <span class="comment">// Return true if an error has occured.</span></div><div class="line">        <span class="keyword">static</span> <span class="keyword">public</span> <span class="keywordtype">bool</span> CheckForError(<span class="keywordtype">string</span> cmd, Canlib.canStatus stat)</div><div class="line">        {</div><div class="line">            <span class="keywordflow">if</span> (stat != Canlib.canStatus.canOK)</div><div class="line">            {</div><div class="line">                Canlib.canGetErrorText(stat, out <span class="keywordtype">string</span> buf);</div><div class="line"></div><div class="line">                Console.WriteLine(<span class="stringliteral">&quot;[{0}] {1}: failed, stat={2}&quot;</span>, cmd, buf, (<span class="keywordtype">int</span>)stat);</div><div class="line">                <span class="keywordflow">return</span> <span class="keyword">true</span>;</div><div class="line">            }</div><div class="line">            <span class="keywordflow">return</span> <span class="keyword">false</span>;</div><div class="line">        }</div><div class="line"></div><div class="line">        <span class="comment">// ListChannels prints a list of all connected CAN interfaces.</span></div><div class="line">        <span class="keyword">static</span> <span class="keyword">public</span> <span class="keywordtype">void</span> ListChannels()</div><div class="line">        {</div><div class="line">            Canlib.canStatus stat;</div><div class="line"></div><div class="line">            <span class="comment">// Get number channels</span></div><div class="line">            stat = Canlib.canGetNumberOfChannels(out <span class="keywordtype">int</span> number_of_channels);</div><div class="line">            <span class="keywordflow">if</span> (CheckForError(<span class="stringliteral">&quot;canGetNumberOfChannels&quot;</span>, stat))</div><div class="line">                <span class="keywordflow">return</span>;</div><div class="line"></div><div class="line">            Console.WriteLine(<span class="stringliteral">&quot;Found {0} channels&quot;</span>, number_of_channels);</div><div class="line"></div><div class="line"></div><div class="line">            <span class="comment">// Loop and print all channels</span></div><div class="line">            <span class="keywordflow">for</span> (<span class="keywordtype">int</span> i = 0; i &lt; number_of_channels; i++)</div><div class="line">            {</div><div class="line">                stat = Canlib.canGetChannelData(i, Canlib.canCHANNELDATA_DEVDESCR_ASCII, out <span class="keywordtype">object</span> device_name);</div><div class="line">                <span class="keywordflow">if</span> (CheckForError(<span class="stringliteral">&quot;canGetChannelData&quot;</span>, stat))</div><div class="line">                    <span class="keywordflow">return</span>;</div><div class="line"></div><div class="line">                stat = Canlib.canGetChannelData(i, Canlib.canCHANNELDATA_CHAN_NO_ON_CARD, out <span class="keywordtype">object</span> device_channel);</div><div class="line">                <span class="keywordflow">if</span> (CheckForError(<span class="stringliteral">&quot;canGetChannelData&quot;</span>, stat))</div><div class="line">                    <span class="keywordflow">return</span>;</div><div class="line"></div><div class="line">                Console.WriteLine(<span class="stringliteral">&quot;Found channel: {0} {1} {2}&quot;</span>, i, device_name, ((UInt32)device_channel + 1));</div><div class="line">            }</div><div class="line">        }</div><div class="line"></div><div class="line">        <span class="keyword">static</span> <span class="keywordtype">void</span> Main(<span class="keywordtype">string</span>[] args)</div><div class="line">        {</div><div class="line">            Canlib.canInitializeLibrary();</div><div class="line"></div><div class="line">            ListChannels();</div><div class="line"></div><div class="line">            <span class="comment">// Press any key to continue</span></div><div class="line">            Console.ReadKey(<span class="keyword">true</span>);</div><div class="line">        }</div><div class="line">    }</div><div class="line">}</div></div><!-- fragment --><p>After adding the code to the project we should now be ready to compile and run, we can do this pressing the green start icon.</p>
<div class="image">
<img src="cs_tutorial_compile_and_run.png" alt="cs_tutorial_compile_and_run.png"/>
<div class="caption">
Compile and run the program by pressing the green start icon</div></div>
<p> <br />
</p>
<div class="image">
<img src="cs_tutorial_program_output.png" alt="cs_tutorial_program_output.png"/>
<div class="caption">
The output from the program should look something like this</div></div>
<h1><a class="anchor" id="section_tutorial_csharp_compiling64"></a>
Setting up 64 bit compiling</h1>
<p>Open the Configuration Manager by selecting it in the dropdown menu. </p><div class="image">
<img src="cs_tutorial_6_configuration_manager_open.png" alt="cs_tutorial_6_configuration_manager_open.png"/>
<div class="caption">
Open the Configuration Manager</div></div>
<p> Create a new solution platform by selecting <code>&lt;New...&gt;</code> in the dropdown menu of "Active solution platform". </p><div class="image">
<img src="cs_tutorial_7_configuration_manager_new_solution_platform_open.png" alt="cs_tutorial_7_configuration_manager_new_solution_platform_open.png"/>
<div class="caption">
Creating a new solution platform</div></div>
<p> Select the x64 option in the dropdown menu and check the checkbox for "Create new project platform". </p><div class="image">
<img src="cs_tutorial_8_configuration_manager_new_solution_platform_x64.png" alt="cs_tutorial_8_configuration_manager_new_solution_platform_x64.png"/>
<div class="caption">
Platform settings</div></div>
<p> Now select the x64 option in the "Active solution platform" dropdown menu and then the x64 in the "Platform" menu. </p><dl class="section note"><dt>Note</dt><dd>The "Active solution platform" is the selection for what you want to compile it for and the Project "platform" is the selection for what you want to compile the project for whith the currently selected Solution platform.</dd></dl>
<div class="image">
<img src="cs_tutorial_9_configuration_manager_x64.png" alt="cs_tutorial_9_configuration_manager_x64.png"/>
<div class="caption">
Making sure the x64 platform have been properly added</div></div>
<p> Select x64 in the dropdown menu and then press start to compile and run. </p><div class="image">
<img src="cs_tutorial_10_compile_selecting_x64.png" alt="cs_tutorial_10_compile_selecting_x64.png"/>
<div class="caption">
Select the x64 option in the dropdown menu</div></div>
</div></div><!-- contents -->
</div><!-- doc-content -->
<!-- HTML footer for doxygen 1.8.13-->
<!-- start footer part -->
<div id="nav-path" class="navpath"><!-- id is needed for treeview function! -->
  <ul>
    <li class="navelem"><a class="el" href="page_tutorial.html">Tutorials</a></li>
    <li class="footer"> (canlib 5.38) Tue Feb 15 2022</li>
  </ul>
</div>
</body>
</html>
